package cn.lyjuan.payment.third.base.model.chl.domain.impl;

import cn.lyjuan.payment.third.base.common.entity.chl.ChlIapEntity;
import cn.lyjuan.payment.third.base.common.enums.impl.ChlEnum;
import cn.lyjuan.payment.third.base.common.mapper.ChlIapEntityMapper;
import cn.lyjuan.payment.third.base.model.chl.domain.IChlCnfModel;
import cn.lyjuan.payment.third.base.pay.chl.impl.iap.bean.IapChlCnf;
import cn.lyjuan.payment.third.base.pay.vo.req.ChlCnf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

/**
 * IAP配置获取类
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class IapChlCnfModelImpl implements IChlCnfModel {
    @Autowired
    private ChlIapEntityMapper mapper;

    private ChlIapEntity entity;

    @Override
    public ChlEnum chl() {
        return ChlEnum.IAP;
    }

    @Override
    public boolean query(Integer appId) {
        Example ex = new Example(ChlIapEntity.class);
        ex.createCriteria()
                .andEqualTo("appId", appId);

        this.entity = this.mapper.selectOneByExample(ex);

        return null != this.entity;
    }

    @Override
    public ChlCnf pkg() {
        IapChlCnf cnf = new IapChlCnf();

        cnf.setChlType(chl().getSimpleChlEnum());
        cnf.setAppId(this.entity.getBundleId());
        cnf.setKey(this.entity.getPassword());
        cnf.setNotifyUrl(this.entity.getNotifyUrl());
        cnf.setReturnUrl(this.entity.getReturnUrl());
        //cnf.setCertPath();

        return cnf;
    }
}
